package com.hpjpw.idoapp.service;

import com.hpjpw.ido.domain.Student;
import com.hpjpw.ido.domain.Teacher;
import com.hpjpw.ido.domain.Worker;
import com.hpjpw.ido.service.IStudentService;
import com.hpjpw.ido.service.ITeacherService;
import com.hpjpw.ido.service.IWorkerService;
import com.hpjpw.idoapp.domain.ReceiveMessageListVo;
import com.hpjpw.idoapp.domain.SendSystemMessageVo;
import com.hpjpw.idoapp.mapper.ReceiveMessageListMapper;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: ruoyi
 * @BelongsPackage: com.hpjpw.idoapp.service
 * @Author: hpj
 * @CreateTime: 2021-01-06 16:17
 * @Description: 系统消息模块相关
 */
@Service
public class ReceiveMessageListService {
    @Autowired
    ReceiveMessageListMapper receiveMessageListMapper;
    @Autowired
    ITeacherService teacherService;
    @Autowired
    IStudentService studentService;
    @Autowired
    IWorkerService workerService;
    /**
     * 通过接收者ID查询消息阅读状态列表
     *
     * @param receiverId 接收者ID
     * @return 消息阅读状态集合
     */
    public List<ReceiveMessageListVo> selectMessageReceiveByReceiverId(Long receiverId) {
        return receiveMessageListMapper.selectMessageReceiveByReceiverId(receiverId);
    }

    /**
     * 通过sendSystemMessageVo获取用户ID列表
     *
     * @param sendSystemMessageVo 接收者ID筛选条件
     * @return 用户ID列表
     */
    public List<Long> getUserIdsFromSendSysMsgVo(SendSystemMessageVo sendSystemMessageVo) {
        List<Long> userIdList = sendSystemMessageVo.getUserIdList();
        if (StringUtils.isNotNull(userIdList) && !userIdList.isEmpty())
            return userIdList;

        userIdList = new ArrayList<>();
        List<Student> studentList;
        List<Teacher> teacherList;
        List<Worker> workerList;

        Student student = new Student();
        Teacher teacher = new Teacher();
        Worker worker = new Worker();

        Long areaId = sendSystemMessageVo.getAreaId();
        if (StringUtils.isNotNull(areaId)) {
            student.setAreaId(areaId);
            teacher.setAreaId(areaId);
        }
        Long enterpriseId = sendSystemMessageVo.getEnterpriseId();
        if (StringUtils.isNotNull(enterpriseId)) {
            student.setEnterpriseId(enterpriseId);
            worker.setEnterpriseId(enterpriseId);
        }

        studentList = studentService.selectStudentList(student);
        teacherList = teacherService.selectTeacherList(teacher);
        workerList = workerService.selectWorkerList(worker);

        //添加接收者Id
        List<String> roleList = sendSystemMessageVo.getRoleList();
        if (StringUtils.isNull(roleList) || roleList.contains("student")) {
            for (Student value : studentList) {
                userIdList.add(value.getUserId());
            }
        }
        if (StringUtils.isNull(roleList) || roleList.contains("teacher")) {
            for (Teacher value : teacherList) {
                userIdList.add(value.getUserId());
            }
        }
        if (StringUtils.isNull(roleList) || roleList.contains("worker")) {
            for (Worker value : workerList) {
                userIdList.add(value.getUserId());
            }
        }

        return userIdList;
    }
    /**
     * 通过消息ID删除消息阅读状态
     *
     * @param messageId 消息ID
     * @return 结果
     */
    public int deleteMessageReceiveByMessageId(Long messageId){
        return receiveMessageListMapper.deleteMessageReceiveByMessageId(messageId);
    }
}
